package com.fmsz.xmly.adapters.broadcast;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Rect;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.fmsz.xmly.R;
import com.fmsz.xmly.entitys.broadcast.CategoriesEntity;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

/**
 * Created by Administrator on 2016/12/6.
 */

public class RecycleViewAdapter extends RecyclerView.Adapter<RecycleViewAdapter.ViewHolder> implements View.OnClickListener {

    private List<CategoriesEntity> listAll;
    private List<CategoriesEntity> closeList;
    private Context context;
    private static final String TAG = "RecycleViewAdapter";
    private boolean isOpen;
    private RecyclerView recycleView;

    public RecycleViewAdapter(Context context) {
        this.context = context;
        listAll = new ArrayList<>();
        closeList = new ArrayList<>();
    }

    @Override
    public int getItemViewType(int position) {
        if (closeList.size()>0 && position==closeList.size()) {
            return R.layout.find_broad_more_item;
        }
        return R.layout.find_broad_item;
    }

    @Override
    public void onAttachedToRecyclerView(RecyclerView recyclerView) {
        super.onAttachedToRecyclerView(recyclerView);
        this.recycleView = recyclerView;
        RecyclerView.ItemDecoration decoration = new RecyclerView.ItemDecoration() {
            @Override
            public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
                super.getItemOffsets(outRect, view, parent, state);
                outRect.set(0,0,2,2);
            }

            @Override
            public void onDraw(Canvas c, RecyclerView parent, RecyclerView.State state) {
                super.onDraw(c, parent, state);
            }

            @Override
            public void onDrawOver(Canvas c, RecyclerView parent, RecyclerView.State state) {
                super.onDrawOver(c, parent, state);
            }
        };
        this.recycleView.addItemDecoration(decoration);
    }

    @Override
    public void onDetachedFromRecyclerView(RecyclerView recyclerView) {
        super.onDetachedFromRecyclerView(recyclerView);
        this.recycleView = null;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(viewType, parent, false);
        view.setOnClickListener(this);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        if (getItemViewType(position)!= R.layout.find_broad_more_item) {
            holder.textView.setText(closeList.get(position).getName());
        }else{
            if (isOpen) {
                holder.moreIcon.setImageResource(R.mipmap.icon_radio_hide);
            }
        }
    }

    @Override
    public int getItemCount() {
        return closeList.size()==0?closeList.size():closeList.size()+1;
    }

    //接受总数据
    public void addAll(Collection<? extends CategoriesEntity> collection){
        int size = closeList.size();
        listAll.addAll(collection);
        for (int i = 0; i < 7; i++) {
            closeList.add(listAll.get(i));
        }
        notifyItemRangeInserted(size,closeList.size()+1);
    }
    //显示隐藏数据
    public void showData(){
        closeList.removeAll(closeList);
        closeList.addAll(listAll);
        notifyDataSetChanged();
    }
    //点击按钮隐藏数据
    public void hideData(){
        closeList.removeAll(closeList);
        for (int i = 0; i < 7; i++) {
            closeList.add(listAll.get(i));
        }
        notifyDataSetChanged();
    }



    @Override
    public void onClick(View v) {
        ImageView icon = (ImageView) v.findViewById(R.id.more_broad_icon);
        if (icon!=null && !isOpen) {
            showData();
            isOpen = true;
        }else if(icon!=null && isOpen){
            icon.setImageResource(R.mipmap.icon_radio_show);
            hideData();
            isOpen = false;
        }
    }

    public static class ViewHolder extends RecyclerView.ViewHolder {
        private final TextView textView;
        private final ImageView moreIcon;

        public ViewHolder(View itemView) {
            super(itemView);
            textView = ((TextView) itemView.findViewById(R.id.broad_item_text));
            moreIcon = ((ImageView) itemView.findViewById(R.id.more_broad_icon));
        }
    }
}
